require(['../config'], () => {
    require(['template', 'header', 'footer'], (template) => {
        class Order {
            constructor() {
                    this.init()
                }
                //初始
            init() {
                //取出localStorage
                this.getcart = JSON.parse(localStorage.getItem('carts'))
                this.orderRender()
                this.numAdd()
                this.moneyAdd()
                this.checkChange()
                this.del()
                this.numConst()
            }

            orderRender() {
                    $('#right-wrap').html(template('bbbbb', { list: this.getcart }))
                }
                //判断check属性
            checkChange() {
                    const _this = this //存一个this
                        //添加change事件   
                    $('.checks').on('change', function() {
                        //从当前的input出发 找到对应的id     
                        const id = $(this).parents('.order-shop').attr('date-id')
                        _this.getcart = _this.getcart.map(shop => {
                                if (shop.id == id) {
                                    shop.check = $(this).prop('checked')
                                }
                                return shop
                            })
                            //把数据存进去
                        localStorage.setItem('carts', JSON.stringify(_this.getcart))
                            //修改数据以后重新计算价格
                        _this.moneyAdd()
                            // _this.setAllcheck()
                        _this.numAdd()
                    })
                }
                //check点击事件
            setAllcheck() {
                // $('#all-check').on('click', function() {
                //     console.log(111)
                // })
                $('#all-check').prop('checked', getcart.every(shop => shop.check))
            }

            numConst() {
                    var num = 0;
                    var _this = this
                    this.getcart = JSON.parse(localStorage.getItem('carts'))
                    $('.foot-jia').on('click', function() {
                        const id = $(this).parents('.order-shop').attr('date-id')
                        let vall = $(this).prev()
                        console.log(vall)
                        _this.getcart = _this.getcart.map(item => {
                            if (id == item.id) {
                                item.const++
                            }
                            return item
                        })
                        localStorage.setItem('carts', JSON.stringify(_this.getcart))
                        var arr = JSON.parse(localStorage.getItem('carts'))

                        for (let i = 0; i < arr.length; i++) {
                            if (arr[i].id == id) {
                                num = arr[i].const
                            }
                        }
                        vall.val(num)
                        _this.moneyAdd()
                        _this.numAdd()
                    })

                    $('.foot-jian').on('click', function() {
                        const id = $(this).parents('.order-shop').attr('date-id')
                        var vall = $(this).next().val()
                        if (vall == 0) {
                            $(this).next().val(0)
                        } else {
                            _this.getcart = _this.getcart.map(item => {
                                if (id == item.id) {
                                    item.const--
                                        $(this).next().val(item.const)
                                }
                                return item
                            })
                            localStorage.setItem('carts', JSON.stringify(_this.getcart))
                            var arr = JSON.parse(localStorage.getItem('carts'))
                            _this.moneyAdd()
                            _this.numAdd()
                        }

                    })
                }
                //商品总价
            moneyAdd() {
                    var arr3 = JSON.parse(localStorage.getItem('carts'))
                    var sum = 0;
                    for (var i = 0; i < arr3.length; i++) {
                        if (arr3[i].check == true) {
                            sum += arr3[i].const * arr3[i].price
                        }
                    }
                    $('.add').html(sum)
                }
                //商品数量
            numAdd() {
                    var arr5 = []
                    var num = this.getcart
                    if (num.length !== 0) {
                        for (let i = 0; i < num.length; i++) {
                            if (num[i].check == true) {
                                arr5.push(num[i])
                            }
                            var num2 = Number(arr5.length)
                            $('.Num').html(num2);
                        }
                    } else {
                        $('.Num').html(0);
                        $('.right-wrap').addClass("active")
                    }
                }
                //删除
            del() {
                var _this = this
                    //获取装li的父元素
                var content = document.querySelector('#right-wrap')
                $('.del').on('click', function() {
                    const id = $(this).parents('.order-shop').attr('date-id')
                    var yuansu = $(this).parents('.order-shop')
                    yuansu = yuansu[0]
                    for (let i = 0; i < _this.getcart.length; i++) {
                        if (_this.getcart[i].id == id) {
                            let index = _this.getcart.indexOf(_this.getcart[i])
                                //通过截取的方式 删除整个li
                            _this.getcart.splice(index, 1)
                            content.removeChild(yuansu)
                        }
                    }
                    //删除后 重新存一下loca数据
                    localStorage.setItem('carts', JSON.stringify(_this.getcart))
                    _this.moneyAdd() //重新渲染总价
                    _this.numAdd() //重新渲染数量
                    if (_this.getcart.length > 0) {

                    } else {
                        // alert('请添加商品')
                    }
                })
            }
        }
        new Order()
    })
})